
<template>
     <el-date-picker
      style="width:100%"
      v-model="currentValue"
      :id="id"
      type="datetime"
      :editable="false"
      value-format="yyyy-MM-dd HH:mm"
      placeholder="选择日期">
    </el-date-picker>
    
</template>
 
<script>
    export default {
        props:['id','styles','value','data','index'],
        name: "lt-datetime",
        data(){
            return {
                currentValue:this.value,
                currentValueOld:this.value,
                pId:"",
                cId:"",
                first:true
            }
        },
        created(){
           
            this.setData();
            
        },
        methods:{
            setData(){
                 if(!this.value){
                    let ids=this.id.split(".");
                    if(ids.length>1){
                        let p=ids[0];
                        let id_=ids[1];
                        this.pId=p;
                        this.cId=id_;
                        if(this.index||this.index=='0'){
                            try{
                                this.currentValue=unescape(this.data[p][this.index][id_]);
                            }catch(e){}
                        }else{
                            try{
                                this.currentValue=unescape(this.data[p][id_]);
                            }catch(e){}
                        }
                    }else{
                        this.currentValue=unescape(this.data[this.id]);
                    }
                    if(!this.style){
                        this.style="width:180px"
                    }
                }else{
                    
                    let data={};
                    data.id=this.id;
                    data.val=this.value;
                    data.index=this.index;
                    this.$emit('input',JSON.stringify(data));
                }

                this.first=false;
                this.currentValueOld=this.currentValue;
            }
        },
        watch:{
            currentValue: function (val) { //currentValue值变动就向父组件传值
                if(this.currentValueOld==val){
                    return;
                }
               
                if(!val){
                    val="";
                }
                let data={};
                data.id=this.id;
                data.val=val;
                data.index=this.index;
               // this.$emit('input',JSON.stringify(data));
                //this.$emit('on-change',JSON.stringify(data));
                let start="";
                let end="";
                if(this.id.indexOf("_dat2")!=-1){
                    end=val;
                }else{
                    start=val;
                }
                console.log("start============="+start+"=="+end);
               
                 if(this.pId){
                     if(this.index!=""&&(this.index==0||this.index>0)){
                        let data_=this.data[this.pId][this.index];
                        data_[this.cId]=val;
                        this.$set(this.data[this.pId],this.index,data_)
                        if(this.id.indexOf("_dat2")!=-1){
                            start=this.data[this.pId][this.index]["lt_dat1"];
                        }else{
                            end=this.data[this.pId][this.index]["lt_dat2"];
                        }
                        
                    }else{
                        this.$set(this.data[this.pId],this.cId,val)
                        if(this.id.indexOf("_dat2")!=-1){
                            start=this.data[this.pId]["lt_dat1"];
                        }else{
                            end=this.data[this.pId]["lt_dat2"];
                        }
                       
                    }
                }else{
                    this.$set(this.data,this.id,val)
                }
                console.log("start======2======="+start+"=="+end);
                if(start&&end){
                    var stime =new Date(start).getTime();
                    var etime = new Date(end).getTime();
                    var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                    //var hours=Math.floor(usedTime/(3600*1000));
                    var hours=Math.ceil(usedTime/(3600*1000));
                    let row=null;
                    let indexs=-1;
                    for(let i=0;i<this.data["item"].length;i++){
                        let item=this.data["item"][i];
                        if(item.szi_ex7=="礼厅"){
                            row=item;
                            indexs=i;
                        }
                    }
                    if(row){
                        let szi_ex2=Number(row.szi_ex2);
                        let szi_ex5=szi_ex2*hours;
                        row.szi_ex4=hours;
                        row.szi_ex5=szi_ex5;
                        this.$set(this.data["item"],indexs,row);
                        let sf_id=row.szi_ex8;

                        this.$post("bygex","/byg/funeral/getSfItemDp",{sf_id:sf_id}).then(data=>{
                            console.log("data======"+data.name)
                            if(data.name){
                                let names=data.name.split(",");
                                for(let i=0;i<names.length;i++){
                                    let n=names[i];
                                    let index_=-1;
                                    let row_=null;
                                    console.log("n============================="+n);
                                    for(let h=0;h<this.data["item"].length;h++){
                                        let item=this.data["item"][h];
                                        if(item.szi_ex1==n){
                                            index_=h;
                                            row_=item;
                                        }
                                    }
                                    console.log(row_,"row_=====ss=s=======")
                                    if(row_){
                                        let szi_ex2_=Number(row_.szi_ex2); 
                                        let szi_ex5_=hours&szi_ex2_;
                                        row_.szi_ex4=hours;
                                        row_.szi_ex5=szi_ex5_;
                                        
                                        this.$set(this.data["item"],index_,row_);
                                    }
                                }
                            }
                        })
                    }
                }
                
                this.frist=false;
            },
            value:function (val) { //对值进行验证0
                
            },
            data:{
                handler: function (newVal) {
                    this.setData()
                },      
                deep: true    //深度监听
            }
        }
    }
</script>
<style scoped>
.formitem{
    margin-left:35px;
}
.formitem2{
    margin-left:10px;
}
</style>
<style >
.el-date-table td.today span{
    
   font-size:18px;
}
.el-date-table td, .el-date-table td div{
    height:20px  !important;  
}
.el-picker-panel__content th{
    background:#409EFF;
    color:#fff !important;  
}
.el-picker-panel__content table{
    border:1px solid #409EFF;
}
.el-date-picker__time-header{
    background:#409EFF;
}
.el-picker-panel el-date-picker el-popper{
    background:#409EFF;
}
</style>